#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <math.h>
#include <algorithm>
using namespace std;

int threeSumClosest(vector<int>& nums, int target) {
    int size=nums.size(),result;
    sort(nums.begin(),nums.end());
    result=target-(nums[0]+nums[1]+nums[2]);
    for(auto temp:nums){
        cout<<temp<<",";
    }
    cout<<"\n";
    int j,k,temp;
    for(int i=0;i<=size-3;i++){
        j=i+1;k=size-1;
        while(j<k){
            temp=target-nums[i]-nums[j]-nums[k];
            if(abs(result)>abs(temp)){
                result=temp;
            }
            cout<<i<<","<<j<<","<<k<<",result:"<<result<<endl;
            if(temp>0){
                j++;
            }
            else if(temp<0){
                k--;
            }
            else return target;
        }
    }
    return target-result;
}

int main(){
//    vector<int>a={-1,0,1,2,-1,-4};
//    vector<int>a={-2,0,0,2,2};
    vector<int>a={-1,2,1,-4};
    cout<<threeSumClosest(a,1)<<endl;

//    vector<vector<int>>result=threeSum1(a);
//    for(int i=0;i<a.size();i++){
//        cout<<result[i][0]<<","<<result[i][1]<<","<<result[i][2]<<endl;
//    }
    return 0;
}